<template>
  <div class="member-order">
    <!-- MemberOrder -->

    <!-- <RouterLink to="/member/order/1001">订单1</RouterLink> -->
    <!-- <RouterLink to="/member/order/1002">订单2</RouterLink> -->

    <!-- 体验jsx语法 -->
    <!-- <XtxTabs v-model="activeName" @tab-click="changeTab">
            <XtxTabsPanel label="选项卡0" name="first">内容0</XtxTabsPanel>
            <XtxTabsPanel v-for="i in 4" :key="i" :label="`选项卡${i}`" :name="`name${i}`">内容{{i}}</XtxTabsPanel>
        </XtxTabs> -->

    <!-- tab组件 -->
    <XtxTabs v-model="activeName" @tab-click="tabClick">
      <XtxTabsPanel
        v-for="item in orderStatus"
        :key="item.name"
        :label="item.label"
        :name="item.name"
      >
      </XtxTabsPanel>
    </XtxTabs>

    <!-- 订单列表 -->
    <div class="order-list">
      <div v-if="loading" class="loading"></div>
      <div class="none" v-if="!loading && orderList.length === 0">暂无数据</div>
      <OrderItem
        @on-delete="handlerDelete"
        @on-cancel="handlerCancel"
        @on-confirm="handlerConfirm"
        @on-logistics="handlerLogistics"
        v-for="item in orderList"
        :key="item.id"
        :order="item"
      />
    </div>

    <!-- 分页组件 -->
    <XtxPagination
      v-if="total > 0"
      @current-change="reqParams.page = $event"
      :total="total"
      :page-size="reqParams.pageSize"
      :current-page="reqParams.page"
    />

    <!-- 取消原因组件 -->
    <!-- ref="orderCancelCom"  绑定数据 -->
    <OrderCancel ref="orderCancelCom" />
    <!-- 查看物流组件 -->
    <OrderLogistics ref="orderLogisticsCom" />
  </div>
</template>
<script>
import { reactive, ref, watch } from "vue";
import { findOrderList, deleteOrder,confirmOrder } from "@/api/order";
import { orderStatus } from "@/api/constants";
import OrderItem from "./components/order-item";
import OrderCancel from "./components/order-cancel.vue";
import OrderLogistics from './components/order-logistics'
import Confirm from '@/components/library/confirm'
import Message from '@/components/library/Message'

export default {
  name: "MemberOrder",
  components: { OrderItem, OrderCancel, OrderLogistics },
  // setup(){
  //     const activeName = ref('name1')
  //     const changeTab=(tab)=>{
  //         console.log(tab);
  //     }
  //     return { activeName,changeTab}
  // }

  setup() {
    const activeName = ref("all");

    // 获取数据
    // 查询订单参数
    // 筛选条件
    const reqParams = reactive({
      page: 1,
      pageSize: 5,
      orderState: 0,
    });

    // 订单列表
    const orderList = ref([]);
    const loading = ref(true);
    // 分页需要一个处理总条数
    const total = ref(0);

    // 因为watch中的immediate: true会立即调用，所以要先声明再调用
    const getOrderList = () => {
      loading.value = true;
      // 查询订单
      findOrderList(reqParams).then((data) => {
        orderList.value = data.result.items;
        total.value = data.result.counts;
        loading.value = false;
      });
    };
    // 筛选条件(点击不同选项卡)变化重新加载
    // 使用侦听器，监听 reqParams 的改变
    watch(reqParams, () => {
      getOrderList()
    }, { immediate: true });

    // 点击选项卡
    const tabClick = (tab) => {
      // console.log(tab);
      // 此时：tab.index 就是订单的状态
      reqParams.orderState = tab.index;
      reqParams.page = 1;
    };

    // 删除订单
    const handlerDelete = (order) => {
      Confirm({ text: "您确认删除该条订单吗？" })
        .then(() => {
          deleteOrder(order.id).then(() => {
            Message({ text: "删除订单成功", type: "success" });
            getOrderList()
          });
        })
        .catch((e) => {});
    };

    return {
      activeName,
      orderStatus,
      orderList,
      tabClick,
      loading,
      total,
      reqParams,
      ...useCancel(),
      handlerDelete,
      ...useConfirm(),
      ...useLogistics()
    };
  },
};

// 取消订单逻辑
export const useCancel = () => {
  // 取消订单组件(组件实例)
  const orderCancelCom = ref(null);
  // 点击取消
  const handlerCancel = (order) => {
    // order 就是你要取消的订单
    // 用order-cancel.vue中的open方法,打开对话框
    orderCancelCom.value.open(order);
  };
  return { orderCancelCom, handlerCancel };
};

// 封装逻辑-确认收货
export const useConfirm = () => {
  const handlerConfirm = (order) => {
    Confirm({ text: '亲，您确认收货吗？确认后货款将打给卖家。' }).then(() => {
      confirmOrder(order.id).then(() => {
        Message({ type: 'success', text: '确认收货成功' })
        // 确认收货后待收货--->待评价
        order.orderState = 4
      })
    }).catch(() => {})
  }
  return { handlerConfirm }
}

// 查看物流逻辑
export const useLogistics = () => {
  const orderLogisticsCom = ref(null)
  const handlerLogistics = (order) => {
    orderLogisticsCom.value.open(order)
  }
  return { handlerLogistics, orderLogisticsCom }
}

</script>
<style scoped lang="less">
.member-order {
  height: 100%;
  background: #fff;
}

.order-list {
  background: #fff;
  padding: 20px;
  position: relative;
  min-height: 400px;
}

.loading {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: rgba(255, 255, 255, 0.9) url(../../../assets/images/loading.gif)
    no-repeat center;
}
.none {
  height: 400px;
  text-align: center;
  line-height: 400px;
  color: #999;
}
</style>